2 * Matt McCutchen's Big Integer Library
5 #include "BigUnsigned.hh"
7 // The "management" routines that used to be here are now in NumberlikeArray.hh.
10 * The steps for construction of a BigUnsigned
11 * from an integral value x are as follows:
12 * 1. If x is zero, create an empty BigUnsigned and stop.
13 * 2. If x is negative, throw an exception.
14 * 3. Allocate a one-block number array.
15 * 4. If x is of a signed type, convert x to the unsigned
16 * type of the same length.
17 * 5. Expand x to a Blk, and store it in the number array.
19 * Since 2005.01.06, NumberlikeArray uses `NULL' rather
20 * than a real array if one of zero length is needed.
21 * These constructors implicitly call NumberlikeArray's
22 * default constructor, which sets `blk = NULL, cap = len = 0'.
23 * So if the input number is zero, they can just return.
24 * See remarks in `NumberlikeArray.hh'.
27 BigUnsigned::BigUnsigned(unsigned long x
) {
29 ; // NumberlikeArray already did all the work
38 BigUnsigned::BigUnsigned(long x
) {
47 throw "BigUnsigned::BigUnsigned(long): Cannot construct a BigUnsigned from a negative number";
50 BigUnsigned::BigUnsigned(unsigned int x
) {
61 BigUnsigned::BigUnsigned(int x
) {
70 throw "BigUnsigned::BigUnsigned(int): Cannot construct a BigUnsigned from a negative number";
73 BigUnsigned::BigUnsigned(unsigned short x
) {
84 BigUnsigned::BigUnsigned(short x
) {
93 throw "BigUnsigned::BigUnsigned(short): Cannot construct a BigUnsigned from a negative number";
98 * The steps for conversion of a BigUnsigned to an
99 * integral type are as follows:
100 * 1. If the BigUnsigned is zero, return zero.
101 * 2. If it is more than one block long or its lowest
102 * block has bits set out of the range of the target
103 * type, throw an exception.
104 * 3. Otherwise, convert the lowest block to the
105 * target type and return it.
109 // These masks are used to test whether a Blk has bits
110 // set out of the range of a smaller integral type. Note
111 // that this range is not considered to include the sign bit.
112 const BigUnsigned::Blk lMask
= ~0 >> 1;
113 const BigUnsigned::Blk uiMask
= (unsigned int)(~0);
114 const BigUnsigned::Blk iMask
= uiMask
>> 1;
115 const BigUnsigned::Blk usMask
= (unsigned short)(~0);
116 const BigUnsigned::Blk sMask
= usMask
>> 1;
119 BigUnsigned::operator unsigned long() const {
123 return (unsigned long) blk
[0];
125 throw "BigUnsigned::operator unsigned long: Value is too big for an unsigned long";
128 BigUnsigned::operator long() const {
131 else if (len
== 1 && (blk
[0] & lMask
) == blk
[0])
132 return (long) blk
[0];
134 throw "BigUnsigned::operator long: Value is too big for a long";
137 BigUnsigned::operator unsigned int() const {
140 else if (len
== 1 && (blk
[0] & uiMask
) == blk
[0])
141 return (unsigned int) blk
[0];
143 throw "BigUnsigned::operator unsigned int: Value is too big for an unsigned int";
146 BigUnsigned::operator int() const {
149 else if (len
== 1 && (blk
[0] & iMask
) == blk
[0])
152 throw "BigUnsigned::operator int: Value is too big for an int";
155 BigUnsigned::operator unsigned short() const {
158 else if (len
== 1 && (blk
[0] & usMask
) == blk
[0])
159 return (unsigned short) blk
[0];
161 throw "BigUnsigned::operator unsigned short: Value is too big for an unsigned short";
164 BigUnsigned::operator short() const {
167 else if (len
== 1 && (blk
[0] & sMask
) == blk
[0])
168 return (short) blk
[0];
170 throw "BigUnsigned::operator short: Value is too big for a short";
174 BigUnsigned::CmpRes
BigUnsigned::compareTo(const BigUnsigned
&x
) const {
175 // A bigger length implies a bigger number.
178 else if (len
> x
.len
)
181 // Compare blocks one by one from left to right.
185 if (blk
[i
] == x
.blk
[i
])
187 else if (blk
[i
] > x
.blk
[i
])
192 // If no blocks differed, the numbers are equal.
197 // PUT-HERE OPERATIONS
200 * Below are implementations of the four basic arithmetic operations
201 * for `BigUnsigned's. Their purpose is to use a mechanism that can
202 * calculate the sum, difference, product, and quotient/remainder of
203 * two individual blocks in order to calculate the sum, difference,
204 * product, and quotient/remainder of two multi-block BigUnsigned
207 * As alluded to in the comment before class `BigUnsigned',
208 * these algorithms bear a remarkable similarity (in purpose, if
209 * not in implementation) to the way humans operate on big numbers.
210 * The built-in `+', `-', `*', `/' and `%' operators are analogous
211 * to elementary-school ``math facts'' and ``times tables''; the
212 * four routines below are analogous to ``long division'' and its
213 * relatives. (Only a computer can ``memorize'' a times table with
214 * 18446744073709551616 entries! (For 32-bit blocks.))
216 * The discovery of these four algorithms, called the ``classical
217 * algorithms'', marked the beginning of the study of computer science.
218 * See Section 4.3.1 of Knuth's ``The Art of Computer Programming''.
222 * On most calls to put-here operations, it's safe to read the inputs little by
223 * little and write the outputs little by little. However, if one of the
224 * inputs is coming from the same variable into which the output is to be
225 * stored (an "aliased" call), we risk overwriting the input before we read it.
226 * In this case, we first compute the result into a temporary BigUnsigned
227 * variable and then copy it into the requested output variable *this.
228 * Each put-here operation uses the DTRT_ALIASED macro (Do The Right Thing on
229 * aliased calls) to generate code for this check.
231 * I adopted this approach on 2007.02.13 (see Assignment Operators in
232 * BigUnsigned.hh). Before then, put-here operations rejected aliased calls
233 * with an exception. I think doing the right thing is better.
235 * Some of the put-here operations can probably handle aliased calls safely
236 * without the extra copy because (for example) they process blocks strictly
237 * right-to-left. At some point I might determine which ones don't need the
238 * copy, but my reasoning would need to be verified very carefully. For now
239 * I'll leave in the copy.
241 #define DTRT_ALIASED(cond, op) \
243 BigUnsigned tmpThis; \
250 void BigUnsigned::add(const BigUnsigned
&a
, const BigUnsigned
&b
) {
251 DTRT_ALIASED(this == &a
|| this == &b
, add(a
, b
));
252 // If one argument is zero, copy the other.
256 } else if (b
.len
== 0) {
261 // Carries in and out of an addition stage
262 bool carryIn
, carryOut
;
265 // a2 points to the longer input, b2 points to the shorter
266 const BigUnsigned
*a2
, *b2
;
267 if (a
.len
>= b
.len
) {
274 // Set prelimiary length and make room in this BigUnsigned
277 // For each block index that is present in both inputs...
278 for (i
= 0, carryIn
= false; i
< b2
->len
; i
++) {
280 temp
= a2
->blk
[i
] + b2
->blk
[i
];
281 // If a rollover occurred, the result is less than either input.
282 // This test is used many times in the BigUnsigned code.
283 carryOut
= (temp
< a2
->blk
[i
]);
284 // If a carry was input, handle it
287 carryOut
|= (temp
== 0);
289 blk
[i
] = temp
; // Save the addition result
290 carryIn
= carryOut
; // Pass the carry along
292 // If there is a carry left over, increase blocks until
293 // one does not roll over.
294 for (; i
< a2
->len
&& carryIn
; i
++) {
295 temp
= a2
->blk
[i
] + 1;
296 carryIn
= (temp
== 0);
299 // If the carry was resolved but the larger number
300 // still has blocks, copy them over.
301 for (; i
< a2
->len
; i
++)
303 // Set the extra block if there's still a carry, decrease length otherwise
311 void BigUnsigned::subtract(const BigUnsigned
&a
, const BigUnsigned
&b
) {
312 DTRT_ALIASED(this == &a
|| this == &b
, subtract(a
, b
));
313 // If b is zero, copy a. If a is shorter than b, the result is negative.
317 } else if (a
.len
< b
.len
)
318 throw "BigUnsigned::subtract: Negative result in unsigned calculation";
320 bool borrowIn
, borrowOut
;
323 // Set preliminary length and make room
326 // For each block index that is present in both inputs...
327 for (i
= 0, borrowIn
= false; i
< b
.len
; i
++) {
328 temp
= a
.blk
[i
] - b
.blk
[i
];
329 // If a reverse rollover occurred, the result is greater than the block from a.
330 borrowOut
= (temp
> a
.blk
[i
]);
331 // Handle an incoming borrow
333 borrowOut
|= (temp
== 0);
336 blk
[i
] = temp
; // Save the subtraction result
337 borrowIn
= borrowOut
; // Pass the borrow along
339 // If there is a borrow left over, decrease blocks until
340 // one does not reverse rollover.
341 for (; i
< a
.len
&& borrowIn
; i
++) {
342 borrowIn
= (a
.blk
[i
] == 0);
343 blk
[i
] = a
.blk
[i
] - 1;
345 // If there's still a borrow, the result is negative.
346 // Throw an exception, but zero out this object first just in case.
349 throw "BigUnsigned::subtract: Negative result in unsigned calculation";
350 } else // Copy over the rest of the blocks
351 for (; i
< a
.len
; i
++)
358 * About the multiplication and division algorithms:
360 * I searched unsucessfully for fast built-in operations like the `b_0'
361 * and `c_0' Knuth describes in Section 4.3.1 of ``The Art of Computer
362 * Programming'' (replace `place' by `Blk'):
364 * ``b_0[:] multiplication of a one-place integer by another one-place
365 * integer, giving a two-place answer;
367 * ``c_0[:] division of a two-place integer by a one-place integer,
368 * provided that the quotient is a one-place integer, and yielding
369 * also a one-place remainder.''
371 * I also missed his note that ``[b]y adjusting the word size, if
372 * necessary, nearly all computers will have these three operations
373 * available'', so I gave up on trying to use algorithms similar to his.
374 * A future version of the library might include such algorithms; I
375 * would welcome contributions from others for this.
377 * I eventually decided to use bit-shifting algorithms. To multiply `a'
378 * and `b', we zero out the result. Then, for each `1' bit in `a', we
379 * shift `b' left the appropriate amount and add it to the result.
380 * Similarly, to divide `a' by `b', we shift `b' left varying amounts,
381 * repeatedly trying to subtract it from `a'. When we succeed, we note
382 * the fact by setting a bit in the quotient. While these algorithms
383 * have the same O(n^2) time complexity as Knuth's, the ``constant factor''
384 * is likely to be larger.
386 * Because I used these algorithms, which require single-block addition
387 * and subtraction rather than single-block multiplication and division,
388 * the innermost loops of all four routines are very similar. Study one
389 * of them and all will become clear.
393 * This is a little inline function used by both the multiplication
394 * routine and the division routine.
396 * `getShiftedBlock' returns the `x'th block of `num << y'.
397 * `y' may be anything from 0 to N - 1, and `x' may be anything from
400 * Two things contribute to this block:
402 * (1) The `N - y' low bits of `num.blk[x]', shifted `y' bits left.
404 * (2) The `y' high bits of `num.blk[x-1]', shifted `N - y' bits right.
406 * But we must be careful if `x == 0' or `x == num.len', in
407 * which case we should use 0 instead of (2) or (1), respectively.
409 * If `y == 0', then (2) contributes 0, as it should. However,
410 * in some computer environments, for a reason I cannot understand,
411 * `a >> b' means `a >> (b % N)'. This means `num.blk[x-1] >> (N - y)'
412 * will return `num.blk[x-1]' instead of the desired 0 when `y == 0';
413 * the test `y == 0' handles this case specially.
415 inline BigUnsigned::Blk
getShiftedBlock(const BigUnsigned
&num
,
416 BigUnsigned::Index x
, unsigned int y
) {
417 BigUnsigned::Blk part1
= (x
== 0 || y
== 0) ? 0 : (num
.blk
[x
- 1] >> (BigUnsigned::N
- y
));
418 BigUnsigned::Blk part2
= (x
== num
.len
) ? 0 : (num
.blk
[x
] << y
);
419 return part1
| part2
;
423 void BigUnsigned::multiply(const BigUnsigned
&a
, const BigUnsigned
&b
) {
424 DTRT_ALIASED(this == &a
|| this == &b
, multiply(a
, b
));
425 // If either a or b is zero, set to zero.
426 if (a
.len
== 0 || b
.len
== 0) {
434 * For each 1-bit of `a' (say the `i2'th bit of block `i'):
435 * Add `b << (i blocks and i2 bits)' to *this.
437 // Variables for the calculation
441 bool carryIn
, carryOut
;
442 // Set preliminary length and make room
445 // Zero out this object
446 for (i
= 0; i
< len
; i
++)
448 // For each block of the first number...
449 for (i
= 0; i
< a
.len
; i
++) {
450 // For each 1-bit of that block...
451 for (i2
= 0; i2
< N
; i2
++) {
452 if ((a
.blk
[i
] & (Blk(1) << i2
)) == 0)
455 * Add b to this, shifted left i blocks and i2 bits.
456 * j is the index in b, and k = i + j is the index in this.
458 * `getShiftedBlock', a short inline function defined above,
459 * is now used for the bit handling. It replaces the more
460 * complex `bHigh' code, in which each run of the loop dealt
461 * immediately with the low bits and saved the high bits to
462 * be picked up next time. The last run of the loop used to
463 * leave leftover high bits, which were handled separately.
464 * Instead, this loop runs an additional time with j == b.len.
465 * These changes were made on 2005.01.11.
467 for (j
= 0, k
= i
, carryIn
= false; j
<= b
.len
; j
++, k
++) {
469 * The body of this loop is very similar to the body of the first loop
470 * in `add', except that this loop does a `+=' instead of a `+'.
472 temp
= blk
[k
] + getShiftedBlock(b
, j
, i2
);
473 carryOut
= (temp
< blk
[k
]);
476 carryOut
|= (temp
== 0);
481 // No more extra iteration to deal with `bHigh'.
482 // Roll-over a carry as necessary.
483 for (; carryIn
; k
++) {
485 carryIn
= (blk
[k
] == 0);
489 // Zap possible leading zero
490 if (blk
[len
- 1] == 0)
495 * DIVISION WITH REMAINDER
496 * The functionality of divide, modulo, and %= is included in this one monstrous call,
497 * which deserves some explanation.
499 * The division *this / b is performed.
500 * Afterwards, q has the quotient, and *this has the remainder.
501 * Thus, a call is like q = *this / b, *this %= b.
503 * This seemingly bizarre pattern of inputs and outputs has a justification. The
504 * ``put-here operations'' are supposed to be fast. Therefore, they accept inputs
505 * and provide outputs in the most convenient places so that no value ever needs
506 * to be copied in its entirety. That way, the client can perform exactly the
507 * copying it needs depending on where the inputs are and where it wants the output.
508 * A better name for this function might be "modWithQuotient", but I would rather
509 * not change the name now.
511 void BigUnsigned::divideWithRemainder(const BigUnsigned
&b
, BigUnsigned
&q
) {
513 * Defending against aliased calls is a bit tricky because we are
514 * writing to both *this and q.
516 * It would be silly to try to write quotient and remainder to the
517 * same variable. Rule that out right away.
520 throw "BigUnsigned::divideWithRemainder: Cannot write quotient and remainder into the same variable";
522 * Now *this and q are separate, so the only concern is that b might be
523 * aliased to one of them. If so, use a temporary copy of b.
525 if (this == &b
|| &q
== &b
) {
527 divideWithRemainder(tmpB
, q
);
532 * Note that the mathematical definition of mod (I'm trusting Knuth) is somewhat
533 * different from the way the normal C++ % operator behaves in the case of division by 0.
534 * This function does it Knuth's way.
536 * We let a / 0 == 0 (it doesn't matter) and a % 0 == a, no exceptions thrown.
537 * This allows us to preserve both Knuth's demand that a mod 0 == a
538 * and the useful property that (a / b) * b + (a % b) == a.
546 * If *this.len < b.len, then *this < b, and we can be sure that b doesn't go into
547 * *this at all. The quotient is 0 and *this is already the remainder (so leave it alone).
555 * At this point we know *this > b > 0. (Whew!)
561 * For each appropriate i and i2, decreasing:
562 * Try to subtract (b << (i blocks and i2 bits)) from *this.
563 * (`work2' holds the result of this subtraction.)
564 * If the result is nonnegative:
565 * Turn on bit i2 of block i of the quotient q.
566 * Save the result of the subtraction back into *this.
568 * Bit i2 of block i remains off, and *this is unchanged.
570 * Eventually q will contain the entire quotient, and *this will
571 * be left with the remainder.
573 * We use work2 to temporarily store the result of a subtraction.
574 * work2[x] corresponds to blk[x], not blk[x+i], since 2005.01.11.
575 * If the subtraction is successful, we copy work2 back to blk.
576 * (There's no `work1'. In a previous version, when division was
577 * coded for a read-only dividend, `work1' played the role of
578 * the here-modifiable `*this' and got the remainder.)
580 * We never touch the i lowest blocks of either blk or work2 because
581 * they are unaffected by the subtraction: we are subtracting
582 * (b << (i blocks and i2 bits)), which ends in at least `i' zero blocks.
584 // Variables for the calculation
588 bool borrowIn
, borrowOut
;
591 * Make sure we have an extra zero block just past the value.
593 * When we attempt a subtraction, we might shift `b' so
594 * its first block begins a few bits left of the dividend,
595 * and then we'll try to compare these extra bits with
596 * a nonexistent block to the left of the dividend. The
597 * extra zero block ensures sensible behavior; we need
598 * an extra block in `work2' for exactly the same reason.
600 * See below `divideWithRemainder' for the interesting and
601 * amusing story of this section of code.
603 Index origLen
= len
; // Save real length.
604 // 2006.05.03: Copy the number and then change the length!
605 allocateAndCopy(len
+ 1); // Get the space.
606 len
++; // Increase the length.
607 blk
[origLen
] = 0; // Zero the extra block.
609 // work2 holds part of the result of a subtraction; see above.
610 Blk
*work2
= new Blk
[len
];
612 // Set preliminary length for quotient and make room
613 q
.len
= origLen
- b
.len
+ 1;
615 // Zero out the quotient
616 for (i
= 0; i
< q
.len
; i
++)
619 // For each possible left-shift of b in blocks...
623 // For each possible left-shift of b in bits...
624 // (Remember, N is the number of bits in a Blk.)
630 * Subtract b, shifted left i blocks and i2 bits, from *this,
631 * and store the answer in work2. In the for loop, `k == i + j'.
633 * Compare this to the middle section of `multiply'. They
634 * are in many ways analogous. See especially the discussion
635 * of `getShiftedBlock'.
637 for (j
= 0, k
= i
, borrowIn
= false; j
<= b
.len
; j
++, k
++) {
638 temp
= blk
[k
] - getShiftedBlock(b
, j
, i2
);
639 borrowOut
= (temp
> blk
[k
]);
641 borrowOut
|= (temp
== 0);
644 // Since 2005.01.11, indices of `work2' directly match those of `blk', so use `k'.
646 borrowIn
= borrowOut
;
648 // No more extra iteration to deal with `bHigh'.
649 // Roll-over a borrow as necessary.
650 for (; k
< origLen
&& borrowIn
; k
++) {
651 borrowIn
= (blk
[k
] == 0);
652 work2
[k
] = blk
[k
] - 1;
655 * If the subtraction was performed successfully (!borrowIn),
656 * set bit i2 in block i of the quotient.
658 * Then, copy the portion of work2 filled by the subtraction
659 * back to *this. This portion starts with block i and ends--
660 * where? Not necessarily at block `i + b.len'! Well, we
661 * increased k every time we saved a block into work2, so
662 * the region of work2 we copy is just [i, k).
665 q
.blk
[i
] |= (Blk(1) << i2
);
673 // Zap possible leading zero in quotient
674 if (q
.blk
[q
.len
- 1] == 0)
676 // Zap any/all leading zeros in remainder
678 // Deallocate temporary array.
679 // (Thanks to Brad Spencer for noticing my accidental omission of this!)
684 * The out-of-bounds accesses story:
686 * On 2005.01.06 or 2005.01.07 (depending on your time zone),
687 * Milan Tomic reported out-of-bounds memory accesses in
688 * the Big Integer Library. To investigate the problem, I
689 * added code to bounds-check every access to the `blk' array
690 * of a `NumberlikeArray'.
692 * This gave me warnings that fell into two categories of false
693 * positives. The bounds checker was based on length, not
694 * capacity, and in two places I had accessed memory that I knew
695 * was inside the capacity but that wasn't inside the length:
697 * (1) The extra zero block at the left of `*this'. Earlier
698 * versions said `allocateAndCopy(len + 1); blk[len] = 0;'
699 * but did not increment `len'.
701 * (2) The entire digit array in the conversion constructor
702 * ``BigUnsignedInABase(BigUnsigned)''. It was allocated with
703 * a conservatively high capacity, but the length wasn't set
704 * until the end of the constructor.
706 * To simplify matters, I changed both sections of code so that
707 * all accesses occurred within the length. The messages went
708 * away, and I told Milan that I couldn't reproduce the problem,
709 * sending a development snapshot of the bounds-checked code.
711 * Then, on 2005.01.09-10, he told me his debugger still found
712 * problems, specifically at the line `delete [] work2'.
713 * It was `work2', not `blk', that was causing the problems;
714 * this possibility had not occurred to me at all. In fact,
715 * the problem was that `work2' needed an extra block just
716 * like `*this'. Go ahead and laugh at me for finding (1)
717 * without seeing what was actually causing the trouble. :-)
719 * The 2005.01.11 version fixes this problem. I hope this is
720 * the last of my memory-related bloopers. So this is what
721 * starts happening to your C++ code if you use Java too much!
725 void BigUnsigned::bitAnd(const BigUnsigned
&a
, const BigUnsigned
&b
) {
726 DTRT_ALIASED(this == &a
|| this == &b
, bitAnd(a
, b
));
727 len
= (a
.len
>= b
.len
) ? b
.len
: a
.len
;
730 for (i
= 0; i
< len
; i
++)
731 blk
[i
] = a
.blk
[i
] & b
.blk
[i
];
736 void BigUnsigned::bitOr(const BigUnsigned
&a
, const BigUnsigned
&b
) {
737 DTRT_ALIASED(this == &a
|| this == &b
, bitOr(a
, b
));
739 const BigUnsigned
*a2
, *b2
;
740 if (a
.len
>= b
.len
) {
748 for (i
= 0; i
< b2
->len
; i
++)
749 blk
[i
] = a2
->blk
[i
] | b2
->blk
[i
];
750 for (; i
< a2
->len
; i
++)
756 void BigUnsigned::bitXor(const BigUnsigned
&a
, const BigUnsigned
&b
) {
757 DTRT_ALIASED(this == &a
|| this == &b
, bitXor(a
, b
));
759 const BigUnsigned
*a2
, *b2
;
760 if (a
.len
>= b
.len
) {
768 for (i
= 0; i
< b2
->len
; i
++)
769 blk
[i
] = a2
->blk
[i
] ^ b2
->blk
[i
];
770 for (; i
< a2
->len
; i
++)
776 // Bitwise shift left
777 void BigUnsigned::bitShiftLeft(const BigUnsigned
&a
, unsigned int b
) {
778 DTRT_ALIASED(this == &a
, bitShiftLeft(a
, b
));
779 Index shiftBlocks
= b
/ N
;
780 unsigned int shiftBits
= b
% N
;
781 // + 1: room for high bits nudged left into another block
782 len
= a
.len
+ shiftBlocks
+ 1;
785 for (i
= 0; i
< shiftBlocks
; i
++)
787 for (j
= 0, i
= shiftBlocks
; j
<= a
.len
; j
++, i
++)
788 blk
[i
] = getShiftedBlock(a
, j
, shiftBits
);
789 // Zap possible leading zero
790 if (blk
[len
- 1] == 0)
794 // Bitwise shift right
795 void BigUnsigned::bitShiftRight(const BigUnsigned
&a
, unsigned int b
) {
796 DTRT_ALIASED(this == &a
, bitShiftRight(a
, b
));
797 // This calculation is wacky, but expressing the shift as a left bit shift
798 // within each block lets us use getShiftedBlock.
799 Index rightShiftBlocks
= (b
+ N
- 1) / N
;
800 unsigned int leftShiftBits
= N
* rightShiftBlocks
- b
;
801 // Now (N * rightShiftBlocks - leftShiftBits) == b
802 // and 0 <= leftShiftBits < N.
803 if (rightShiftBlocks
>= a
.len
+ 1) {
804 // All of a is guaranteed to be shifted off, even considering the left
809 // Now we're allocating a positive amount.
810 // + 1: room for high bits nudged left into another block
811 len
= a
.len
+ 1 - rightShiftBlocks
;
814 for (j
= rightShiftBlocks
, i
= 0; j
<= a
.len
; j
++, i
++)
815 blk
[i
] = getShiftedBlock(a
, j
, leftShiftBits
);
816 // Zap possible leading zero
817 if (blk
[len
- 1] == 0)
821 // INCREMENT/DECREMENT OPERATORS
824 void BigUnsigned::operator ++() {
827 for (i
= 0; i
< len
&& carry
; i
++) {
829 carry
= (blk
[i
] == 0);
832 // Matt fixed a bug 2004.12.24: next 2 lines used to say allocateAndCopy(len + 1)
833 // Matt fixed another bug 2006.04.24:
834 // old number only has len blocks, so copy before increasing length
835 allocateAndCopy(len
+ 1);
841 // Postfix increment: same as prefix
842 void BigUnsigned::operator ++(int) {
847 void BigUnsigned::operator --() {
849 throw "BigUnsigned::operator --(): Cannot decrement an unsigned zero";
852 for (i
= 0; borrow
; i
++) {
853 borrow
= (blk
[i
] == 0);
856 // Zap possible leading zero (there can only be one)
857 if (blk
[len
- 1] == 0)
861 // Postfix decrement: same as prefix
862 void BigUnsigned::operator --(int) {